package com.bupt.controller.common;

import com.bupt.annotation.AccessLimit;
import com.bupt.constants.ResponseResult;
import com.bupt.dto.sys.UserDto;
import com.bupt.service.common.LinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = {"项目链路的操作"})
public class LinkController {

    @Resource
    private LinkService linkService;

    @ApiOperation("添加FIBER链路")
    @AccessLimit(interfaceName = "添加FIBER链路")
    @PostMapping("/projects/{projectId}/fiberLinks")
    public Object addFiberLink(@ApiIgnore UserDto userDto,
                               @PathVariable("projectId") String projectId,
                               @RequestParam("name") String fiberName,
                               @RequestParam("code")String code,
                               @RequestParam("sourceId") String sourceId,
                               @RequestParam("targetId") String targetId,
                               @RequestParam(value = "xxsl")Integer xxsl,
                               @RequestParam(value = "asfs") String asfs,
                               @RequestParam(value = "xxsh") Double xxsh,
                               @RequestParam(value = "length") long length,
                               @RequestParam(value = "year") int year,
                               @RequestParam(value = "serviceLength") int serviceLength,
                               @RequestParam(value = "faultTime",required = false) Integer fualtTime,
                               @RequestParam(value = "aveAttenua",required = false) Double aveAttenua
    ){
        try {
            Object o = linkService.addFiberLink(userDto,projectId, fiberName,code, sourceId, targetId, xxsl,asfs, xxsh, length,year, serviceLength,fualtTime, aveAttenua);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("删除FIBER链路")
    @AccessLimit(interfaceName = "删除FIBER链路")
    @DeleteMapping (value = "/projects/{projectId}/fiberLinks/{id}")
    public Object deleteFiberLink(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String fiberId){
        try{
        Object o = linkService.deleteFiberLink(userDto, projectId, fiberId);
        return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("更新fiber链路")
    @AccessLimit(interfaceName = "更新fiber链路")
    @PutMapping(value = "/projects/{projectId}/fiberLinks/{id}")
    public Object updateFiberLink(@ApiIgnore UserDto userDto,
                                  @PathVariable("projectId") String projectId,
                                  @PathVariable("id") String fiberLinkId,
                                  @RequestParam("name") String fiberName,
                                  @RequestParam("code")String code,
                                  @RequestParam(value = "xxsl") int xxsl,
                                  @RequestParam(value = "length") long length,
                                  @RequestParam(value = "year") int year,
                                  @RequestParam(value = "serviceLength") int serviceLength,
                                  @RequestParam(value = "asfs") String asfs,
                                  @RequestParam(value = "faultTime") Integer fualtTime,
                                  @RequestParam(value = "aveAttenua") Double aveAttenua,
                                  @RequestParam(value = "xxsh") Double xxsh){
        try {
            Object o = linkService.updateFiberLink(userDto, projectId, fiberLinkId, fiberName,code, xxsl, length, year, serviceLength, asfs, fualtTime, aveAttenua,xxsh);
            return ResponseResult.createOK(o);

        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("获取项目的FIBER链路")
    @AccessLimit(interfaceName = "获取项目的FIBER链路")
    @GetMapping("/projects/{projectId}/fiberLinks")
    public Object getFiberLinksList(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId){
        try {
            Object o = linkService.getFiberLinksList(userDto, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());

        }
    }

    @ApiOperation("创建wdm的链路")
    @AccessLimit(interfaceName = "创建一条新 wdm 链路")
    @PostMapping("/projects/{projectId}/wdmLinks")
    public Object addWdmLink(@ApiIgnore UserDto userDto,
                             @PathVariable("projectId")String projectId,
                             @RequestParam("name")String linkName,
                             @RequestParam("code")String code,
                             @RequestParam("sourceId")String sourceId,
                             @RequestParam("targetId")String targetId,
                             @RequestParam("size")long size,
                             @RequestParam("length")long length,
                             @RequestParam(value = "year",required = false) int year,
                             @RequestParam(value = "rate",required = false)String rate
                             ){
        try {
            Object o = linkService.addWdmLink(userDto, projectId, linkName, code, sourceId, targetId, size, length, year, rate);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());

        }
    }


    @ApiOperation("获取项目的WDM链路")
    @AccessLimit(interfaceName = "获取项目的WDM链路")
    @GetMapping("/projects/{projectId}/wdmLinks")
    public Object getWdmLinksList(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId){
        try {
            Object o = linkService.getWdmLinksList(userDto, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }



    @ApiOperation("更新项目WDM的链路")
    @AccessLimit(interfaceName = "创建项目WDM链路")
    @PutMapping("/projects/{projectId}/wdmLinks/{id}")
    public Object updateWdmLink(@ApiIgnore UserDto userDto,
                                @PathVariable("projectId") String projectId,
                                @PathVariable("id") String linkId,
                                @RequestParam("code")String code,
                                @RequestParam("name")String linkName,
                                @RequestParam("year")int year,
                                @RequestParam("size")int size,
                                @RequestParam("rate")String rate,
                                @RequestParam("length")long length){
        try{
            Object o = linkService.updateWdmLink(userDto,projectId, linkId,code, linkName, year, size, rate, length);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("删除WDM链路")
    @AccessLimit(interfaceName = "删除WDM链路")
    @DeleteMapping (value = "/projects/{projectId}/wdmLinks/{id}")
    public Object deleteWdmLink(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.deleteWdmLink(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }



    @ApiOperation("创建otn的链路")
    @AccessLimit(interfaceName = "创建otn链路")
    @PostMapping("/projects/{projectId}/otnLinks")
    public Object addOtnLink(@ApiIgnore UserDto userDto,
                             @PathVariable("projectId")String projectId,
                             @RequestParam("name")String linkName,
                             @RequestParam("code")String code,
                             @RequestParam("sourceId")String sourceId,
                             @RequestParam("targetId")String targetId,
                             @RequestParam(value = "year",required = false) int year,
                             @RequestParam("loadOnLayer")String loadOnlayer,
                             @RequestParam("rate")String rate ,
                             @RequestParam("length")long length
    ){
        try {
            Object o = linkService.addOtnLink(userDto, projectId, linkName, code, sourceId, targetId, year, loadOnlayer, rate, length);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());

        }
    }

    @ApiOperation("获取项目的otn链路")
    @AccessLimit(interfaceName = "获取项目的otn链路")
    @GetMapping("/projects/{projectId}/otnLinks")
    public Object getOtnLinksList(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId){
        try {
            Object o = linkService.getOtnLinksList(userDto, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }


    @ApiOperation("更新项目otn的链路")
    @AccessLimit(interfaceName = "更新项目otn链路")
    @PutMapping("/projects/{projectId}/otnLinks/{id}")
    public Object updateOtnLink(@ApiIgnore UserDto userDto,
                                @PathVariable("projectId") String projectId,
                                @PathVariable("id") String linkId,
                                @RequestParam("code")String code,
                                @RequestParam("name")String linkName,
                                @RequestParam(value = "year",required = false)int year,
                                @RequestParam("loadOnLayer")String loadOnlayer,
                                @RequestParam("rate")String rate,
                                @RequestParam("length")long length){
        try{
            Object o = linkService.updateOtnLink(userDto,projectId, linkId,code, linkName, year, loadOnlayer, rate, length);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("删除项目的OTN链路")
    @AccessLimit(interfaceName = "删除OTN链路")
    @DeleteMapping (value = "/projects/{projectId}/otnLinks/{id}")
    public Object deleteOtnLink(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.deleteOtnLink(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }



    @ApiOperation("创建sdh的链路")
    @AccessLimit(interfaceName = "创建sdh链路")
    @PostMapping("/projects/{projectId}/sdhLinks")
    public Object addSdhLink(@ApiIgnore UserDto userDto,
                             @PathVariable("projectId")String projectId,
                             @RequestParam("name")String linkName,
                             @RequestParam("code")String code,
                             @RequestParam("sourceId")String sourceId,
                             @RequestParam("targetId")String targetId,
                             @RequestParam(value = "year",required = false) int year,
                             @RequestParam("loadOnLayer")String loadOnlayer,
                             @RequestParam("rate")String rate ,
                             @RequestParam("length")long length
    ){
        try {
            Object o = linkService.addSdhLink(userDto, projectId, linkName, code, sourceId, targetId, year, loadOnlayer, rate, length);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());

        }
    }

    @ApiOperation("获取项目的智能sdh链路")
    @AccessLimit(interfaceName = "获取项目的智能sdh链路")
    @GetMapping("/projects/{projectId}/sdhLinks")
    public Object getSdhLinksList(@ApiIgnore UserDto userDto,@PathVariable("projectId") String projectId){
        try {
            Object o = linkService.getSdhLinksList(userDto, projectId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }



    @ApiOperation("更新项目的智能sdh的链路")
    @AccessLimit(interfaceName = "更新项目的智能sdh链路")
    @PutMapping("/projects/{projectId}/sdhLinks/{id}")
    public Object updateSdhLink(@ApiIgnore UserDto userDto,
                                @PathVariable("projectId") String projectId,
                                @PathVariable("id") String linkId,
                                @RequestParam("code")String code,
                                @RequestParam("name")String linkName,
                                @RequestParam(value = "year",required = false)int year,
                                @RequestParam("loadOnLayer")String loadOnlayer,
                                @RequestParam("rate")String rate,
                                @RequestParam("length")long length) {
        try {
            Object o = linkService.updateSdhLink(userDto, projectId, linkId, code, linkName, year, loadOnlayer, rate, length);
            return ResponseResult.createOK(o);
        } catch (Exception e) {
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("删除项目的智能SDH链路")
    @AccessLimit(interfaceName = "删除SDH链路")
    @DeleteMapping (value = "/projects/{projectId}/sdhLinks/{id}")
    public Object deleteSdhLink(UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.deleteSdhLink(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("获取项目Wdm链路的层间路由")
    @AccessLimit(interfaceName = "查询Wdm链路的层间路由")
    @GetMapping(value = "/projects/{projectId}/wdmLinks/{id}")
    public Object getLinksLoadMessageWdm(UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.getLinksLoadMessageWdm(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("获取项目OTN链路的层间路由")
    @AccessLimit(interfaceName = "查询OTN链路的层间路由")
    @GetMapping(value = "/projects/{projectId}/otnLinks/{id}")
    public Object getLinksLoadMessageOtn(UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.getLinksLoadMessageOtn(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }

    @ApiOperation("获取项目的查询SDH链路的层间路由")
    @AccessLimit(interfaceName = "查询SDH链路的层间路由")
    @GetMapping(value = "/projects/{projectId}/sdhLinks/{id}")
    public Object getLinksLoadMessageSdh(UserDto userDto,@PathVariable("projectId") String projectId,@PathVariable("id") String linkId){
        try{
            Object o = linkService.getLinksLoadMessageSdh(userDto, projectId, linkId);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }


 /*   @ApiOperation("修改链路的承载")
    @AccessLimit(interfaceName = "修改链路的承载")
    @PostMapping("/projects/{projectId}/{linkId}/updateLoadOnLink")
    public Object modRoute(UserDto userDto,
                           @PathVariable("projectId") String projectId,
                           @PathVariable("linkId") String linkId,
                           @RequestParam(value = "link1",defaultValue = "") String link1,
                           @RequestParam(value = "link2",defaultValue = "") String link2,
                           @RequestParam(value = "link3",defaultValue = "") String link3,
                           @RequestParam(value = "link4",defaultValue = "") String link4,
                           @RequestParam(value = "link5",defaultValue = "") String link5){
        try{
            String route = link1+","+link2+","+link3+","+link4+","+link5;
            Object o = linkService.modRoute(userDto, projectId, linkId,route);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }*/

    @ApiOperation("修改链路的承载")
    @AccessLimit(interfaceName = "修改链路的承载")
    @PostMapping("/projects/{projectId}/{linkId}/updateLoadOnLink")
    public Object updateLoadOnLink(@ApiIgnore UserDto userDto,
                                   @PathVariable("projectId") String projectId,
                                   @PathVariable("linkId") String linkId,
                                   @RequestParam("link1") String link1,
                                   @RequestParam("link2") String link2,
                                   @RequestParam("link3") String link3,
                                   @RequestParam("link4") String link4,
                                   @RequestParam("link5") String link5 ){
        try {
            Object o = linkService.updateLoadOnLink(userDto,projectId,linkId,link1,
                    link2,link3 ,link4,link5);
            return ResponseResult.createOK(o);
        }catch (Exception e){
            return ResponseResult.createFail(e.getMessage());
        }
    }
    }



















